﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace XPathAndLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            //Write a program, which extract from the file catalog.xml the prices for all albums, published before 5 years or more. 
            //XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load("../../catalog.xml");
            //string xPathQuery = "/albums/album[year<2005]";

            //XmlNodeList albumList = xmlDoc.SelectNodes(xPathQuery);
            //foreach (XmlNode beerNode in albumList)
            //{
            //    string year = beerNode.SelectSingleNode("year").InnerText;
            //    string price = beerNode.SelectSingleNode("price").InnerText;
            //    Console.WriteLine(year+","+price);
            //}


            //Linq
            XDocument xmlDoc = XDocument.Load("../../catalog.xml");
            var albums =
                from album in xmlDoc.Descendants("album")
                where int.Parse(album.Element("year").Value)<2005
                select new
                {
                    Name = album.Element("name").Value,
                    Price = album.Element("price").Value,
                    Year = album.Element("year").Value
                };
            Console.WriteLine("Found {0} albums:", albums.Count());
            foreach (var album in albums)
            {
                Console.WriteLine(" - {0} {1}лв {2}", album.Name, album.Price,album.Year);
            }
        }
    }
}
